<?php

/**
 * CommonConfigBehavior is a behavior for the application.
 * It loads additional config parameters that cannot be statically
 * written in config/main
 */
class CommonConfigBehavior extends CBehavior
{

    /**
     * Declares events and the event handler methods
     * See yii documentation on behavior
     */
    public function events()
    {
        return array_merge(parent::events(), array(
            'onBeginRequest' => 'beginRequest',
        ));
    }

    /**
     * Load configuration that cannot be put in config/main
     */
    public function beginRequest()
    {
        if (isset($_POST['LANG_CODE']))
            $this->owner->user->setState('LANG_CODE', $_POST['LANG_CODE']);
        if ($this->owner->user->getState('LANG_CODE'))
            $this->owner->language = $this->owner->user->getState('LANG_CODE');
        else
            $this->owner->language = 'en';
    }

    /**
     * get best selling information
     * @return CActiveRecord
     */
    public function getBestSeller(){
            $criteria=new CDbCriteria;
            $criteria->select =  array("FN_QTC_MULTI_NAME(P.PRODUCT_NAME,P.PRODUCT_ENG_NAME,:LANG_CODE) AS PRODUCT_NAME",
                                        "FN_QTC_MULTI_NAME(P.SLUG,P.SLUG_ENG,:LANG_CODE) AS SLUG");
            $criteria->condition = "BEST_SELL_YN = 'Y' ";
            $criteria->alias = "P";
            $criteria->params =  array(':LANG_CODE'=> Yii::app()->language);
            $model = Product::model()->with(array("Menu", array("select"=>
                array("FN_QTC_MULTI_NAME(Menu.MENU_SLUG,Menu.MENU_SLUG_ENG,:LANG_CODE) AS SLUG"))))->findAll($criteria);
            return $model;
    }

    /**
     * get best selling information
     * @return CActiveRecord
     */
    public function getLastest(){
        $criteria=new CDbCriteria;
        $criteria->select =  array("FN_QTC_MULTI_NAME(P.PRODUCT_NAME,P.PRODUCT_ENG_NAME,:LANG_CODE) AS PRODUCT_NAME",
                                    "FN_QTC_MULTI_NAME(P.SLUG,P.SLUG_ENG,:LANG_CODE) AS SLUG");
        $criteria->condition = "BEST_SELL_YN = 'Y' ";
        $criteria->alias = "P";
        $criteria->limit = 10;
        $criteria->params =  array(':LANG_CODE'=> Yii::app()->language);
        $model = Product::model()->with(array("Menu", array("select"=>array("FN_QTC_MULTI_NAME(Menu.MENU_SLUG,Menu.MENU_SLUG_ENG,:LANG_CODE) AS MENU_SLUG"))))->findAll($criteria);
        return $model;
    }
}

?>